<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">



    function test_Layer_TileCache_constructor (t) {
        t.plan( 1 );

        var name = 'Test Layer';
        var url = "http://labs.metacarta.com/wms-c/Basic.py/";
        var layername = "basic";
        var options = {'type':'png'}; 

        var layer = new OpenLayers.Layer.TileCache(name, url, layername, options);
        t.ok( layer instanceof OpenLayers.Layer.TileCache, "returns OpenLayers.Layer.TileCache object" );
        layer.destroy();
    }

    function test_Layer_TileCache_clone(t) {
        t.plan(3);

        var name = 'Test Layer';
        var url = "http://labs.metacarta.com/wms-c/Basic.py/";
        var layername = "basic";
        var options = {'type':'png'}; 
        var layer = new OpenLayers.Layer.TileCache(name, url, layername, options);

        var clone = layer.clone();
        t.eq(layer.name, clone.name, "clone() correctly copy the 'name' property");
        t.eq(layer.url, clone.url, "clone() correctly copy the 'url' property");
        t.eq(layer.layername, clone.layername, "clone() correctly copy the 'layername' property");
        clone.destroy();
        layer.destroy();
    }

    function test_Layer_TileCache_clearTiles (t) {
        t.plan( 1 );
        var name = 'Test Layer';
        var url = "http://labs.metacarta.com/wms-c/Basic.py/";
        var layername = "basic";
        var options = {'type':'png'}; 
        var layer = new OpenLayers.Layer.TileCache(name, url, layername, options);
        var map = new OpenLayers.Map('map');
        map.addLayer(layer);

        map.setCenter(new OpenLayers.LonLat(0,0));

        //grab a reference to one of the tiles
        var tile = layer.grid[0][0];        

        layer.clearGrid();

        t.ok( layer.grid != null, "layer.grid does not get nullified" );
        map.destroy();
    }


    function test_Layer_TileCache_getTileCacheBounds(t) {
        t.plan( 1 );

        var name = 'Test Layer';
        var url = "http://labs.metacarta.com/wms-c/Basic.py/";
        var layername = "basic";
        var options = {'type':'png'}; 
        var layer = new OpenLayers.Layer.TileCache(name, url, layername, options);

        var bl = { bounds: new OpenLayers.Bounds(1,2,2,3)};
        var tr = { bounds: new OpenLayers.Bounds(2,3,3,4)};
        layer.grid = [ [6, tr], 
                       [bl, 7]];

        var bounds = layer.getTilesBounds();
    
        var testBounds = new OpenLayers.Bounds(1,2,3,4);
        
        t.ok( bounds.equals(testBounds), "getTileCacheBounds() returns correct bounds")

    }

    function test_Layer_TileCache_getResolution(t) {
        t.plan( 1 );

        var name = 'Test Layer';
        var url = "http://labs.metacarta.com/wms-c/Basic.py/";
        var layername = "basic";
        var options = {'type':'png', maxResolution: 180/256}; 
        var map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.TileCache(name, url, layername, options);
        map.addLayer(layer);

        map.zoom = 5;

        t.eq( layer.getResolution(), 0.02197265625, "getResolution() returns correct value");
        map.destroy();
    }

    function test_Layer_TileCache_getZoomForExtent(t) {
        t.plan( 2 );
        var bounds, zoom;

        var name = 'Test Layer';
        var url = "http://labs.metacarta.com/wms-c/Basic.py/";
        var layername = "basic";
        var options = {'type':'png', maxResolution: 180/256}; 
        var map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.TileCache(name, url, layername, options);
        map.addLayer(layer);

        bounds = new OpenLayers.Bounds(10,10,12,12);
        zoom = layer.getZoomForExtent(bounds);

        t.eq( zoom, 7, "getZoomForExtent() returns correct value");

        bounds = new OpenLayers.Bounds(10,10,100,100);
        zoom = layer.getZoomForExtent(bounds);

        t.eq( zoom, 1, "getZoomForExtent() returns correct value");
        map.destroy();
    }   

    function test_Layer_TileCache_getURL(t) {

        t.plan(2);
        
        var map = new OpenLayers.Map('map');
        var name = 'Test Layer';
        var url = "http://labs.metacarta.com/wms-c/Basic.py/";
        var layername = "basic";
        var options = {'layername':'basic', 'format':'image/jpg', maxResolution: 180/256}; 
        var layer = new OpenLayers.Layer.TileCache(name, url, layername, options);
        map.addLayer(layer);
        map.setCenter(new OpenLayers.LonLat(0,0), 9);
        var tileurl = layer.getURL(new OpenLayers.Bounds(3.515625,45,4.21875,45.703125));
        t.eq(tileurl, "http://labs.metacarta.com/wms-c/Basic.py/basic/09/000/000/522/000/000/384.jpeg", "Tile URL is correct");

        layer.url = ["http://tilecache1/", "http://tilecache2/", "http://tilecache3/"];
        tileurl = layer.getURL(new OpenLayers.Bounds(3.515625,45,4.21875,45.703125));
        t.eq(tileurl, "http://tilecache2/basic/09/000/000/522/000/000/384.jpeg", "Tile URL is deterministic");
        map.destroy();
    }

    function test_Layer_TileCache_serverResolutions(t) {
        t.plan(2);
        
        var map = new OpenLayers.Map('map', {
            resolutions: [13,11]
        });

        var layer = new OpenLayers.Layer.TileCache('tc layer', '', 'basic');
        map.addLayer(layer);
        map.setCenter(new OpenLayers.LonLat(0,0), 1);

        var tileurl = layer.getURL(new OpenLayers.Bounds(0,0,0,0));
        var level = parseInt(tileurl.split('/')[2]);
        t.eq(map.getZoom(), level, "Tile zoom level is correct without serverResolutions");

        layer.serverResolutions = [14,13,12,11,10];
        tileurl = layer.getURL(new OpenLayers.Bounds(0,0,0,0));
        level = parseInt(tileurl.split('/')[2]);
        var gotLevel = OpenLayers.Util.indexOf(layer.serverResolutions, map.getResolution());
        t.eq(gotLevel, level, "Tile zoom level is correct with serverResolutions");
        
        map.destroy();
    }

    function test_Layer_TileCache_destroy (t) {

        t.plan( 3 );

        var name = 'Test Layer';
        var url = "http://labs.metacarta.com/wms-c/Basic.py/";
        var layername = "basic";
        var options = {'layername':'basic', 'format':'image/jpg', maxResolution: 180/256}; 
        var map = new OpenLayers.Map('map');
        var layer = new OpenLayers.Layer.TileCache(name, url, layername, options);
        map.addLayer(layer);
        layer.destroy();
        t.eq( layer.grid, null, "layer.grid is null after destroy" );
        t.eq( layer.tileSize, null, "layer.tileSize is null after destroy" );


    //test with tile creation
        layer = new OpenLayers.Layer.TileCache(name, url, options);
        map.addLayer(layer);
        map.setCenter(new OpenLayers.LonLat(0,0), 5);
        //grab a reference to one of the tiles
        var tile = layer.grid[0][0];        

        layer.destroy();

        t.ok( layer.grid == null, "tiles appropriately destroyed");
        map.destroy();
    }
    

  </script>
</head>
<body>
<div id="map" style="width:500px;height:550px"></div>
</body>
</html>
